package com.yida.system.api.factory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import com.yida.common.core.domain.R;
import com.yida.system.api.RemoteSmsService;
import com.yida.system.api.domain.SysSms;
import com.yida.system.api.domain.SysUser;

/**
 * 短信服务降级处理
 * 
 * @author hmh
 * @date 2022/10/26
 */
@Component
public class RemoteSmsFallbackFactory implements FallbackFactory<RemoteSmsService> {
    private static final Logger log = LoggerFactory.getLogger(RemoteSmsFallbackFactory.class);

    @Override
    public RemoteSmsService create(Throwable throwable) {
        log.error("短信服务调用失败:{}", throwable.getMessage());
        return new RemoteSmsService() {
            @Override
            public R<SysSms> sendMsgCode(SysUser sysUser, String source) {
                return R.fail("发送短信失败:{}" + throwable.getMessage());
            }

            @Override
            public R<Boolean> removeSmsCode(String phonenumber, String source) {
                log.error(phonenumber + "：删除短信失败:{}" + throwable.getMessage());
                return R.ok();
            }

            @Override
            public R<SysSms> sendSmsByPhonenumber(SysSms sysSms, String source) {
                return R.fail("短信内容发送失败:{}" + throwable.getMessage());
            }

            @Override
            public R<Boolean> removeSms(String phonenumber, Integer smsType, String smsSource, String source) {
                log.error(phonenumber + "：[" + smsType + ":" + smsSource + "]删除短信失败:{}" + throwable.getMessage());
                return R.ok();
            }

            @Override
            public R<SysSms> sendAuthSmsByPhonenumber(SysSms sysSms, String source) {
                return R.fail("短信内容发送失败:{}" + throwable.getMessage());
            }

        };
    }
}
